<template>
  <div>
    <div class="iptWp" v-if="type == 'input'">
      <input type="text" v-maxLength="maxLength" v-model="ipt" />
      <span class="span">0/{{ maxLength }}</span>
    </div>
    <div class="iptWp" v-else-if="type == 'textarea'">
      <textarea v-maxLength="maxLength" v-model="ipt" cols="30" rows="10"></textarea>
      <span class="spantextarea">0/{{ maxLength }}</span>
    </div>
  </div>
</template>

<script>
export default {
    data(){
        return{
            ipt:''
        }
    },
  props: {
    type: {
      default: "input",
      type: String
    },
    maxLength: {
      default: 10,
      type: [String, Number]
    }
  },
  directives: {
    maxLength: {
      inserted(el, binding) {
        el.setAttribute("maxlength", binding.value);
      },
      update(el, binding) {
        let valueLen = el.value.length;
        let span = el.nextElementSibling;
        span.innerHTML = valueLen + "/" + binding.value;
        if (valueLen == binding.value) {
          el.style.color = "red";
        } else {
          el.style.color = "";
        }
      }
    }
  }
};
</script>

<style scoped>
</style>